<?php

include_once ('../utilities.php')

?>
<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="../reset.css">
	<link rel="stylesheet" type="text/css" href="../mainStyle.css">
	<link rel="stylesheet" type="text/css" href="../searchButton.css">
	<title>SHPE</title>
	<style>
		td, th {
			padding: 2px 10px;
			text-align: left;
		}
	</style>
</head>
<body>

	<div id="container">
		<?php

$root = '../';
include_once ('../header.php')

?>
        
		<div id="page-body">
            <?php

if (!empty($_SESSION['LoggedIn']) && !empty($_SESSION['User']))
{
    $_SESSION['LastViewedChapterID'] = $_GET["id"];

    $editPermissions = false;
    $deletePermissions = false;

    $user = unserialize($_SESSION['User']);
    $permissions = $user->getPermissions();
    $query = "SELECT * FROM Permissions WHERE PermissionID = " . $permissions;
    $result = $mysqli->query($query);
    if ($result->num_rows > 0 && $user->getChapter()->getChapterID() == $_GET["id"])
    {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        switch ($row['Title'])
        {
            case "President":
            case "Vice-President":
            case "Web-Admin":
                $editPermissions = true;
                $deletePermissions = true;
                break;
        }
    }

    // User is logged in, display chapter.
    $query = "SELECT * FROM Chapters WHERE ChapterID = " . $_GET["id"];
    $result = $mysqli->query($query);
    if ($result->num_rows < 1)
    {
        echo "The chapter requested does not exist.";
    }
    // Else, show the chapter.
    else
    {
        $chapterRow = $result->fetch_array(MYSQLI_ASSOC);

?>
					<h1>SHPE @ <?=

        $chapterRow['ChapterName']

?></h1>
					<br />
					<p><?=

        $chapterRow['Description']

?></p>
					<br /><br />
					<h2>Our E-Board Members</h2>
					<br />
					<table style="width:90%;">
					<?

        //Get the eboard members
        $query = "SELECT * FROM Permissions WHERE PermissionID > 0 AND PermissionID < 6";
        $data = $mysqli->query($query);
        if (!empty($data))
        {
            //For each title (pres, vp, etc.)
            $memberCount = 0;
            while ($arr = $data->fetch_array(MYSQLI_ASSOC))
            {
                $query = "SELECT * FROM Users WHERE PermissionID = '" . $arr['PermissionID'] .
                    "' AND ChapterID = '" . $chapterRow['ChapterID'] . "'";
                $result = $mysqli->query($query);
                $row = $result->fetch_array(MYSQLI_ASSOC);
                if (!empty($row))
                {
                    //Assumes 1 user per title, display their info

                    // Check if start of new row.
                    if (($memberCount % 2) == 0)
                    {
                        // Start new table row.


?>
									<tr>
									<?php

                    }

                    // Build column for current member.
                    $eboarduser = $factory->getUserByEmail($row['Email'], $row['Password'], true);

?>
								<td>
									<div style="border:2px solid #E5CA19; margin-bottom:20px; padding:2px; width:90%; background:#FFF9BA;">
										<div style="display:inline-block;">
											<img src="../images/default-profile-img.png" alt="Member Profile Image" width="96" height="96">
										</div>
										<div style="display:inline-block; vertical-align:top;">
											<h3><?=

                    $arr['Title']

?></h3>
											<br />
											<?=

                    $eboarduser->getFirstName()

?> <?=

                    $eboarduser->getLastName()

?>
											<br />
											<?=

                    $eboarduser->getEmail()

?>
										</div>
									</div>
								</td>
								<?php

                    // Check if end of row.
                    if (($memberCount % 2) == 1)
                    {
                        // End the table row.


?>
									</tr>
									<?php

                    }

                    ++$memberCount;
                }
            }
        }

?>
					</table>
					
					<br />
					
					<h2>Upcoming Events</h2>
					<br />
					
					<?php

        // Build the events table in PHP.

        $query = "SELECT * FROM Events WHERE ChapterID = " . $chapterRow['ChapterID'] .
            " ORDER BY EventDate";
        $data = $mysqli->query($query);
        if (!empty($data))
        {
            if ($data->num_rows > 0)
            {
                // Start the table.


?>
							<table>
								<tr>
									<th>Date</th>
									<th>Event</th>
								</tr>
							<?php

                while ($arr = $data->fetch_array(MYSQLI_ASSOC))
                {
                    $query = "SELECT * FROM Events WHERE EventID = " . $arr['EventID'];
                    $result = $mysqli->query($query);
                    $row = $result->fetch_array(MYSQLI_ASSOC);
                    if (!empty($row))
                    {

?>
									<tr>
										<td><?=

                        date('m/d/Y', strtotime($row['EventDate']))

?></td>
										<td><a href="<?=

                        $root

?>event/?id=<?=

                        $row['EventID']

?>"><?=

                        $row['Name']

?></a></td>
									</tr>
									<?php

                    }
                }
                // Finish the table.


?>
							</table>
							<?

            } else
            {

?>
							This chapter doesn't have any events currently.
							<?php

            }
        }

        if ($editPermissions)
        {

?>
						<br />
						<form name="create-event" action="<?=

            $root

?>event/?id=-1" method="post">
							<input type="submit" name="new-event" value="Create Event" class="editButton">
						</form>
						<?php

        }

?>
					
				<br /><br /><br />
				<h2>Our Projects</h2>
				<br />
				
				<?php

        // Build the projects table in PHP.

        $query = "SELECT * FROM Projects WHERE ChapterID = " . $chapterRow['ChapterID'] .
            " ORDER BY Name";
        $data = $mysqli->query($query);
        if (!empty($data))
        {
            if ($data->num_rows > 0)
            {
                // Start the table.


?>
							<table>
							<?php

                while ($arr = $data->fetch_array(MYSQLI_ASSOC))
                {
                    $query = "SELECT * FROM Projects WHERE ProjectID = " . $arr['ProjectID'];
                    $result = $mysqli->query($query);
                    $row = $result->fetch_array(MYSQLI_ASSOC);
                    if (!empty($row))
                    {

?>
										<tr>
											<td><a href="<?=

                        $root

?>project/?id=<?=

                        $row['ProjectID']

?>"><?=

                        $row['Name']

?></a></td>
										</tr>
									<?php

                    }
                }
                // Finish the table.


?>
							</table>
							<?

            } else
            {

?>
							This chapter doesn't have any projects currently.
							<?php

            }
        }

        if ($editPermissions)
        {

?>
						<br />
						<form name="create-project" action="<?=

            $root

?>project/?id=-1" method="post">
							<input type="submit" name="new-project" value="Create Project" class="editButton">
						</form>
						<?php

        }
    }
} else
{

?>
				<p> You must be logged in to view this page. </p>
				<?php

}

?>
		</div>
		
		<?php

include_once ('../footer.php')

?>
	</div>

</body>
</html>